import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
import '../../l10n/app_localizations.dart';


class WebViewPayPage extends StatefulWidget {
  final String url;
  final void Function(String)? onOrderIdReceived;

  const WebViewPayPage({
    Key? key,
    required this.url,
    this.onOrderIdReceived,
  }) : super(key: key);

  @override
  _WebViewPagePayState createState() => _WebViewPagePayState();
}

class _WebViewPagePayState extends State<WebViewPayPage> {
  late WebViewController _controller; // 用于控制 WebView 的对象
  late WebViewWidget _webView; // 用于渲染 WebView 的 Widget

  @override
  void initState() {
    super.initState();
    // 初始化 WebView
    _controller = WebViewController()
      ..setJavaScriptMode(JavaScriptMode.unrestricted)
      ..addJavaScriptChannel('appCallback', onMessageReceived: (JavaScriptMessage message) {
        widget.onOrderIdReceived!("1");
        Navigator.pop(context);
      })
      ..loadRequest(Uri.parse(widget.url)); // 加载传入的URL

    _webView = WebViewWidget(controller: _controller); // 使用 WebViewWidget 显示 WebView
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: IconButton(
          icon: const Icon(Icons.arrow_back),
          onPressed: () {
            widget.onOrderIdReceived!("1");
            Navigator.pop(context);
          },
        ),
        title: Text(AppLocalizations.of(context)!.checkstand,style: const TextStyle(fontSize: 17)),
      ),
      body: PopScope(
        canPop: false,
        onPopInvokedWithResult: (didPop, result) {
          if (!didPop) {
            widget.onOrderIdReceived!("1");
            Navigator.pop(context);
          }
        },
        child: _webView,
      ),
    );
  }
}
